from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from home.models import BannerModel
from home.models import NavModel
from home.serializers import NavSerializer, BanerSerializer
from fgww.utils import constants
from django.core.cache import cache

# Create your views here.
#
# 导航的视图
# 顶部导航


class NavHeardListViews(ListAPIView):
    queryset = NavModel.objects.filter( isshow=True, position=constants.NAV_HEARD).order_by('-id')[
               :constants.NAV_HEARD_SIZE]
    serializer_class = NavSerializer

    def list(self, request, *args, **kwargs):
        heard = 'heard'
        cache_data = cache.get(heard)
        if cache_data:
            return Response({"msg":"OK","data":cache_data})
        data = NavModel.objects.filter(isshow=True, position=constants.NAV_HEARD).order_by('-id')[
               :constants.NAV_HEARD_SIZE]
        ser = NavSerializer(data,many=True)
        cache.set(heard,ser.data,timeout=60*60)
        return Response({"msg":"ok","data":ser.data})


class NavFooterListViews(ListAPIView):
    queryset = NavModel.objects.filter(isshow=True, position=constants.NAV_FOOTER).order_by('-id')[
               :constants.NVA_FOOTER_SIZE]
    serializer_class = NavSerializer
    def list(self, request, *args, **kwargs):
        foot = 'foot'
        cache_data = cache.get(foot)
        if cache_data:
            return Response({"msg":"ok","data":cache_data})
        data = NavModel.objects.filter(isshow=True, position=constants.NAV_FOOTER).order_by('-id')[
               :constants.NVA_FOOTER_SIZE]
        ser = NavSerializer(data,many=True)
        cache.set(foot,ser.data,timeout=60*60)
        return Response({"msg":"ok","data":ser.data})

# 轮播图视图
class BannerListApiView(ListAPIView):
    queryset = BannerModel.objects.filter(isshow=True).order_by('-id')[:constants.BANNER_SIZE]
    serializer_class = BanerSerializer



    # lookup_field = 'pk'
    # lookup_url_kwarg = None

# 底部导航
